﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TileEngine.tiles;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TileEngine.items.tiles.ores
{
    class DefaultOre:Defaultitem
    {
        int coolDown=0;
        public override void mouseLeftClick(GameTime gameTime)
        {
            if (coolDown < getCoolDown())
            {
                coolDown += gameTime.ElapsedGameTime.Milliseconds;
                return;
            }
            Point mouseTile = new Point((int)((Mouse.GetState().X + Camera.Location.X)/Global.tileSize.X),(int)( (Mouse.GetState().Y + Camera.Location.Y)/Global.tileSize.Y));
            if ((new Vector2(mouseTile.X*Global.tileSize.X,mouseTile.Y*Global.tileSize.Y) - Global.currentWorld.player.Location).Length() < 100)
            {
                if (canPlace(mouseTile))
                {
                    if (Global.currentWorld.player.inventory.getItems(this.GetType(),1))
                    {
                        Itile newTile = Activator.CreateInstance(getTile()) as Itile;
                        newTile.setCoord(mouseTile.X, mouseTile.Y);
                        Global.currentWorld.tiles[mouseTile.X, mouseTile.Y].tile = newTile;
                        coolDown = 0;
                    }
                }

            }
            
        }
        public override void useDraw(SpriteBatch spriteBatch, Color tint)
        {
            spriteBatch.Draw(this.getTexture(), new Rectangle((int)Camera.screenDimension.X / 2, (int)Camera.screenDimension.Y / 2, 20, 20), null, tint, (Global.currentWorld.player.isLookRight ? 1 : -1) *
                (float)coolDown / (float)getCoolDown() - MathHelper.PiOver4, new Vector2(0, Global.tileSize.X), SpriteEffects.None, 0);
            spriteBatch.Draw(Textures.player, new Rectangle((int)Camera.screenDimension.X / 2, (int)Camera.screenDimension.Y / 2 - 3*Global.tileSize.Y / 8, 7, 18), new Rectangle(393, 0, 7, 21), tint, (Global.currentWorld.player.isLookRight ? 1 : -1) *
                2*(float)coolDown / (float)getCoolDown() , new Vector2(3.5f, Global.tileSize.X), SpriteEffects.None, 0);
        }
        int getCoolDown()//miliseconds
        {
            return 200;
        }
        bool canPlace(Point p)
        {
            if (Global.currentWorld.tiles[p.X, p.Y].tile.GetType() != typeof(Empty))
                return false;
            if (Global.currentWorld.tiles[p.X+1, p.Y].tile.GetType() != typeof(Empty))
                return true;
            if (Global.currentWorld.tiles[p.X-1, p.Y].tile.GetType() != typeof(Empty))
                return true;
            if (Global.currentWorld.tiles[p.X, p.Y+1].tile.GetType() != typeof(Empty))
                return true;
            if (Global.currentWorld.tiles[p.X, p.Y-1].tile.GetType() != typeof(Empty))
                return true;
            return false;
        }
    }
}
